#include "ty_scene.h"
#include "tal_log.h"
#include "tal_memory.h"
#include "app_handle.h"
#include "app_light_tools.h"
typedef struct {
    UINT8_T mode;
    UINT8_T gap_time;
    UINT8_T vary_time;
    UINT16_T hue;
    UINT16_T sat;
    UINT16_T val;
    UINT16_T bright;
    UINT16_T temper;
}TY_SCENE_COLOR_S;

typedef struct {
    UINT8_T num;
    UINT8_T color_cnt;
    UINT8_T color_size;
    TY_SCENE_COLOR_S color_list[8];
}TY_SCENE_DATA_S;

TY_SCENE_DATA_S ty_scene_data = {0};
TY_SCENE_DATA_S ty_scene_data_temp = {0};

OPERATE_RET TY_Scene_SetData(UINT8_T *data, UINT8_T len)
{
    UINT8_T high_mode = data[1];
    tal_system_memset(&ty_scene_data_temp, 0, sizeof(TY_SCENE_DATA_S));
    ty_scene_data_temp.num = data[0];
    ty_scene_data_temp.color_size = 0;
    len -= TY_SCENE_HRAD_LENGTH;
    while((len >= TY_SCENEE_UNIT_LENGTH) && (ty_scene_data_temp.color_size < TY_SCENE_MAX_UNIT))
    {
        len -= TY_SCENEE_UNIT_LENGTH;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].mode = ((high_mode & (1 << ty_scene_data_temp.color_size)) >> ty_scene_data_temp.color_size) & 0x01;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].mode = (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].mode << 1) | ((data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH] >> 7) & 0x01);
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].gap_time = data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH] & 0x7F;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].vary_time = data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 1] & 0x7F;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].hue = ((UINT16_T)data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 1] << 1) & 0x0100;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].hue |= data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 2] & 0xFF;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].sat = ((UINT16_T)data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 3] << 2) & 0x0300;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].sat |= data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 4] & 0xFF;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].val = ((UINT16_T)data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 3] << 4) & 0x0300;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].val |= data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 5] & 0xFF;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].bright = ((UINT16_T)data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 3] << 6) & 0x0300;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].bright |= data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 6] & 0xFF;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].temper = ((UINT16_T)data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 3] << 8) & 0x0300;
        ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].temper |= data[TY_SCENE_HRAD_LENGTH + ty_scene_data_temp.color_size * TY_SCENEE_UNIT_LENGTH + 7] & 0xFF;
        TAL_PR_DEBUG("COLOR:%d data: %d %d %d %d %d %d %d %d", ty_scene_data_temp.color_size, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].mode, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].gap_time, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].vary_time, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].hue, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].sat, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].val, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].bright, \
                                      ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].temper);
        ty_scene_data_temp.color_size++;
        if ((ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].mode > 0b11) || \
            (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].gap_time > 100) || \
            (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].vary_time > 100) || \
            (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].hue > CTRL_HSV_H_MAX) || \
            (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].sat > CTRL_CAL_VALUE_RANGE) || \
            (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].val > CTRL_CAL_VALUE_RANGE) || \
            (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].bright > CTRL_CAL_VALUE_RANGE) || \
            (ty_scene_data_temp.color_list[ty_scene_data_temp.color_size].temper > CTRL_CAL_VALUE_RANGE))
        {
            TAL_PR_ERR("ty scene data set err!");
            return OPRT_COM_ERROR;
        }
    }
    if (ty_scene_data_temp.color_size == 0)
    {
        TAL_PR_ERR("ty scene color num err!");
        return OPRT_COM_ERROR;
    }
    tal_system_memcpy(&ty_scene_data, &ty_scene_data_temp, sizeof(TY_SCENE_DATA_S));
    ty_scene_data.color_cnt = 0;
    return OPRT_OK;
}

void TY_Scene_Get(LIGHT_DATA_T *data, uint16_t *cntMax)
{
    UINT8_T cnt = ty_scene_data.color_cnt / 2;
    app_light_tools_BT2CW(ty_scene_data_temp.color_list[cnt].bright, \
                            ty_scene_data_temp.color_list[cnt].temper, \
                             data);
    if (ty_scene_data_temp.color_list[cnt].mode == 0x01)
    {
        if (ty_scene_data.color_cnt % 2 == 0)
        {
            *cntMax = TASK_LOOP_FLAG | 1;
        } else {
            *cntMax = TASK_LOOP_FLAG | (105 - ty_scene_data_temp.color_list[cnt].vary_time) * 10;
        }
    } else if (ty_scene_data_temp.color_list[cnt].mode == 0x02)
    {
        if (ty_scene_data.color_cnt % 2 == 0)
        {
            *cntMax = TASK_LOOP_FLAG | (105 - ty_scene_data_temp.color_list[cnt].gap_time) * 10;
        } else {
            *cntMax = TASK_LOOP_FLAG | (105 - ty_scene_data_temp.color_list[cnt].vary_time) * 10;
        }
    } else {
        *cntMax = (105 - ty_scene_data_temp.color_list[cnt].gap_time) * 10;
    }
    ty_scene_data.color_cnt = (ty_scene_data.color_cnt + 1) % (2 * ty_scene_data.color_size);
}

